<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width: 100%;">
    <el-col>{{ treeNode.name }}</el-col>
    <el-col :span="4">
      <el-row justify="end" type="flex" align="middle">
        <el-col>
          {{ treeNode.manager }}
        </el-col>
        <el-col>
          <el-dropdown @command="handleOperatedepts">
            <span>
              操作 <i class="el-icon-arrow-down" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-if="checkPermission('1')" command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  props: {
    treeNode: {
      require: true,
      type: Object,
      default() {
        return {}
      }
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async handleOperatedepts(type) {
      switch (type) {
        case 'add':
          // 添加子部门
          // 添加子部门 在当前添加的部门下下添加 子部门 => this.treeNode就是当前点击的部门
          this.$emit('addDepts', this.treeNode)
          break
        case 'edit':
          this.$emit('editDepts', this.treeNode)
          break
        case 'del':
          this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            return delDepartments(this.treeNode.id)
          }).then(() => {
            // 只需要等到成功的时候, 调用接口删除了 后端数据变化了 但是前端没变   重新获取
            this.$emit('delDepts')
            this.$message.success('删除成功')
          }).catch(() => {})
          break
      }
    }
  }
}

</script>
<style lang='scss' scoped>
</style>
